package com.polaris.live.ui.main.live

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R.drawable
import com.polaris.live.adapter.live.HomePartyAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.FragmentPartyBinding
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.translation.R.string
import com.polaris.live.utils.LiveUtils
import com.polaris.live.utils.StartCallProxy
import com.polaris.live.utils.interfaces.OnAutoRefreshListener
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.live.HomeLiveViewModel

/**
 * PartyFragment
 *
 * @author Created by 半仙 on 2024/2/27/027 11:07
 */
class PartyFragment : AbstractLiveListFragment<HomeLiveViewModel, FragmentPartyBinding>(),
    OnAutoRefreshListener {

    override val refreshView by lazy { mBinding.refresh }

    override val layoutManager by lazy {
        GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false)
    }

    override val mAdapter by lazy { HomePartyAdapter() }

    override val listLiveData by lazy { mViewModel.mListLiveData }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mBinding.refresh.setEmptyTip(
            drawable.not_hava_people,
            StringUtils.getString(string.none_yet_hava_party)
        )
    }

    override fun initListener() {
        mAdapter.setOnItemClickListener { _, _, position ->
            //加入派对
            val user = mAdapter.data.getOrNull(position) ?: return@setOnItemClickListener
            GlobalPopupManager.showCompletePopup(requireContext(), isOk = {
                if (user is DisplayUserBean) {
                    if (LiveUtils.isToWaiting(user)) {
                        StartCallProxy.builder(this).startCall(
                            mViewModel,
                            UserConst.From.LIVE,
                            UserConst.LastFrom.LIVE,
                            LiveConst.SenderFrom.MANUAL,
                            user.base.userId, user.base
                        )
                    } else {
                        LiveUtils.invokeSpectator(requireContext(), user)
                    }
                }
            })
        }
        mBinding.startParty.setOnSingleClickListener {
            GlobalPopupManager.showCompletePopup(requireContext(), isOk = {
                Router.builder(RoutePath.SET_PARTY_COVER_ACTIVITY)
                    .navigation(requireActivity())
            })
        }
    }
}